/***************************************************************
 * Name:      SetupCreatorApp.cpp
 * Purpose:   Code for Application Class
 * Author:    Jeremie Fouche (jeremie.fouche@gmail.com)
 * Created:   2007-05-05
 * Copyright: Jeremie Fouche ()
 * License:
 **************************************************************/

#ifdef WX_PRECOMP //
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "setupapp.h"
#include "setupframe.h"
#include "sc/interface.h"

#include <wx/cmdline.h>
#include <wx/hashmap.h>

WX_DECLARE_STRING_HASH_MAP(wxLanguage, HashLang);


IMPLEMENT_APP(SetupApp);

const wxString SetupApp::OPTION_LANG = _T("l");

/**
* Start of the application
*
* @return true
*/
bool SetupApp::OnInit()
{
    // Create the main frame window
    m_pFrame = new SetupFrame(NULL);
    m_pFrame->Centre(wxBOTH);
    m_pFrame->Show(true);
    SetTopWindow(m_pFrame);

    // Parse the command line
	return wxApp::OnInit();
}

/**
* Called by the framework (OnInit) to describe the available options
*
* @param parser
*/
void SetupApp::OnInitCmdLine(wxCmdLineParser& parser)
{
    // the standard command line options
    static const wxCmdLineEntryDesc cmdLineDesc[] =
    {
        { wxCMD_LINE_OPTION , OPTION_LANG
        , _T("lang") , _T("langage of the gui")
        , wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL
        }
        ,
        { wxCMD_LINE_PARAM, NULL
        , NULL, _T("File to open.")
        , wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL
        }
        ,
        { wxCMD_LINE_NONE } // terminator
    };

    parser.SetDesc(cmdLineDesc);
}

/**
* Called by the framework (OnInit) to parse command line
*
* @param parser
*
* @return true if options are OK
*/
bool SetupApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
    bool bParsed = wxApp::OnCmdLineParsed(parser);
    if (bParsed)
    {
        if (parser.GetParamCount() == 1)
        {
            int error = m_pFrame->LoadContext(parser.GetParam());
            bParsed = (error == sc::error::NONE);
        }

        // Internationalization
        wxString sLang;
        if (parser.Found(OPTION_LANG, &sLang))
        {
            wxLogMessage(wxT("SetupApp::OnCmdLineParsed - Lang=%s"), sLang.c_str());

            bParsed = false;

            HashLang hashLang;
            hashLang[ _T("fr") ] = wxLANGUAGE_FRENCH;

            HashLang::const_iterator iterLang = hashLang.find(sLang);
            if (iterLang != hashLang.end())
            {
                if (m_locale.Init(iterLang->second, wxLOCALE_CONV_ENCODING))
                {
                    wxLocale::AddCatalogLookupPathPrefix(wxT("."));
                    bParsed = m_locale.AddCatalog(wxT("setup"));

                    wxLogMessage(wxT("SetupApp::OnCmdLineParsed - Lang=%s => %s"), sLang.c_str(), bParsed ? wxT("OK") : wxT("KO !"));
                }
            }
        }
    }
    return bParsed;
}
